class Solution {
public:
    int countSubstrings(string s) {
        int n = s.size();
        vector<vector<bool>> dp(n + 1, vector<bool>(n + 1));
        int ret = 0;
        for (int i = n - 1; i >= 0; --i) 
        {
            for (int j = i; j < n; ++j) 
            {
                if (s[i] == s[j]) 
                {
                    if(i==j)
                        dp[i][j] = true;
                    if (s[i + 1] == s[j])
                        dp[i][j] = true;
                    if (i + 1 < j)
                        dp[i][j] = dp[i + 1][j - 1];
                    if (dp[i][j])
                        ret++;
                }
            }
        }
        return ret;
    }
};
